﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SuperDarts.Gameplay.Modes;
using SuperDarts.ScreenManagement;
using SuperDarts.Screens.Menus;

namespace SuperDarts.Screens.GameScreens
{
    public class GameplayScreen : GameScreen
    {
        private Texture2D _background;
        private ContentManager _content;
        private Dartboard _dartboard;
        private SerialManager.DartRegisteredDelegate _dartRegisteredDelegate;
        public GameMode Mode;
        private Dartboard.SegmentClickedDelegate _segmentClickedDelegate;
        private bool _showDartboard;

        public GameplayScreen(GameMode mode)
        {
            mode.GameplayScreen = this;
            Mode = mode;
        }

        public override void LoadContent()
        {
            if (_content == null)
            {
                _content = new ContentManager(SuperDarts.ScreenManager.Game.Services, @"Content");
            }

            _background = _content.Load<Texture2D>(@"Images\Backgrounds\AbstractBackground"); // SuperDarts.Options.Theme

            Mode.LoadContent(_content);

            SuperDarts.SoundManager.PlaySound(SoundCue.GameStart);

            _dartRegisteredDelegate = Mode.RegisterDart;
            SerialManager.Instance().OnDartRegistered = _dartRegisteredDelegate;
            SerialManager.Instance().OnDartHit = null;

            _dartboard = new Dartboard();
            _dartboard.LoadContent(_content);
            _segmentClickedDelegate = dartboard_OnSegmentClicked;
            _dartboard.OnSegmentClicked += _segmentClickedDelegate;

            _dartboard.Scale = 0.5f;
        }

        private void dartboard_OnSegmentClicked(IntPair segment)
        {
            Mode.RegisterDart(segment.X, segment.Y);
        }

        public override void UnloadContent()
        {
            SerialManager.Instance().OnDartRegistered = null;
            base.UnloadContent();
            _content.Unload();
        }

        public void Pause()
        {
            var pause = new PauseMenuScreen(this);
            SuperDarts.ScreenManager.AddScreen(pause);
        }

        public override void HandleInput(InputState inputState)
        {
            base.HandleInput(inputState);

            if (inputState.MenuCancel)
            {
                Pause();
            }

            if (inputState.IsKeyPressed(Keys.F6))
            {
                _showDartboard = !_showDartboard;
            }

            if (_showDartboard)
            {
                _dartboard.HandleInput(inputState);
            }

            if (inputState.IsKeyPressed(Keys.Space))
            {
                Mode.ForcePlayerChange();
            }
        }

        public override void Update(GameTime gameTime, bool isCoveredByOtherScreen)
        {
            base.Update(gameTime, isCoveredByOtherScreen);
            Mode.Update(gameTime, isCoveredByOtherScreen);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);

            spriteBatch.Begin();
            spriteBatch.Draw(_background, new Rectangle(0, 0, SuperDarts.Viewport.Width, SuperDarts.Viewport.Height),
                Mode.CurrentPlayer.Color);
            spriteBatch.End();

            Mode.Draw(spriteBatch);

            if (_showDartboard)
            {
                _dartboard.Position = new Vector2(SuperDarts.Viewport.Width, SuperDarts.Viewport.Height)*0.5f;
                _dartboard.Draw(spriteBatch);
            }
        }
    }
}